package com.wcity.manage.web.Announcement;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Announcement;
import com.wcity.manage.dao.model.SeedAnn;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.announcement.AnnouncementService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 公告管理
 * Created by zm on 2016/12/15.
 */
@Controller
public class AnnouncementAct {

    @Resource private AnnouncementService announcementService;

    /**
     * 分页查询公告列表
     * @param ann
     * @param pageModel
     * @param request
     * @return
     */
    @RequestMapping ("ann_list")
    public String AnnList (Announcement ann , PageModel pageModel, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        ann.setUserId(user.getId());
        pageModel.setParams( ann );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "Announcement", ann );
        request.setAttribute( "userId", user.getId() );
        request.setAttribute( "blockName", announcementService.getBlockName(user.getId()));
        request.setAttribute( "list", announcementService.getPayAnnListPage( pageModel ) );
        return "announcement/annList";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping ("to_ann_add")
    public String toAnnAdd () {
        return "announcement/annAdd";
    }

    /**
     * 添加公告
     * @param ann
     */
    @RequestMapping ("ann_add")
    public void annAdd (Announcement ann , HttpServletResponse response, HttpServletRequest request) {

        String code = "200";
        String meg = "修改成功";
        try{
            User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
            ann.setUserId(user.getId());
            ann.setCreateTime(new Date());
            int success = announcementService.annAdd (ann);
            if( success == 0 ){
                code = "300";
                meg = "修改失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "修改失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "ann_list", "", "closeCurrent", ""));
    }

    /**
     * 跳转到修改页面
     * @param request
     * @return
     */
    @RequestMapping ("to_ann_edit")
    public String toAnnEdit ( HttpServletRequest request ) {
        request.setAttribute( "annInfo", announcementService.getAnnInfo (request.getParameter("id")) );
        return "announcement/annEdit";
    }

    /**
     * 修改
     * @param ann
     * @param response
     */
    @RequestMapping ("ann_edit")
    public void annEdit ( Announcement ann , HttpServletResponse response ) {

        String code = "200";
        String meg = "修改成功";
        try{
            int success = announcementService.annEdit ( ann );
            if( success == 0 ){
                code = "300";
                meg = "修改失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "修改失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "ann_list", "", "closeCurrent", ""));
    }

    /**
     * 删除
     * @param request
     */
    @RequestMapping ("ann_delect")
    public void annDelect ( HttpServletRequest request , HttpServletResponse response ) {

        String code = "200";
        String meg = "删除成功";
        try{
            int success = announcementService.annDelect ( request.getParameter("id") );
            if( success == 0 ){
                code = "300";
                meg = "删除失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "删除失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "ann_list", "", "", ""));
    }

    /**
     * 跳转到发送公告页面
     * @param request
     * @return
     */
    @RequestMapping ("to_send_Ann")
    public String toSendAnn ( HttpServletRequest request ) {
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        request.setAttribute( "list", announcementService.getBlockName(user.getId()));
        request.setAttribute( "annId", request.getParameter("id"));
        return "announcement/annSend";
    }

    /**
     * 发送公告
     * @param seedAnn
     * @param response
     */
    @RequestMapping ("send_ann")
    public void sendAnn (SeedAnn seedAnn , HttpServletResponse response ,HttpServletRequest request) {
        String[] blockId = request.getParameter("blockId").split(",");
        for(String id : blockId){
            seedAnn.setBlockId(id);
            seedAnn.setSendTime(new Date());
            seedAnn.setAnnId(Integer.parseInt(request.getParameter("annId")));
            announcementService.sendAnn ( seedAnn );
        }
        RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "发送成功", "", "", "", "" ) );
    }
}
